home *** CD-ROM | disk | FTP | other *** search
- const CSSXULNS = "@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul);";
- const CSSHTMLNS = "@namespace url(http://www.w3.org/1999/xhtml);";
- var style = null;
- var initialCode = "";
- var appliedCode = null;
- var updateOriginalCode = false;
- var updateURL = null;
- var triggeringDocument = null;
- var installPingURL = null;
- var newStyle = false;
- var keepStyleOnClose = false;
- var descriptionElement, codeElement, enabledElement, neverUpdateElement, STRINGS, URLS, prefs;
- try {
- Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader).loadSubScript('chrome://itsalltext/content/API.js');
- } catch(e) {}
- function init() {
- STRINGS = document.getElementById("strings");
- URLS = document.getElementById("urls");
- descriptionElement = document.getElementById("description");
- codeElement = document.getElementById("code");
- enabledElement = document.getElementById("enabled");
- neverUpdateElement = document.getElementById("allow-updates");
- prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService);
- prefs = prefs.getBranch("extensions.googleredesigned.");
- var wrapLines = prefs.getBoolPref("wrap_lines");
- refreshWordWrap(wrapLines);
- document.getElementById("wrap-lines").checked = wrapLines;
- var rainbowPicker = document.getElementById("rainbowpicker-detect");
- if (getComputedStyle(rainbowPicker, "").MozBinding == "url(chrome://rainbowpicker/content/colorpicker.xml#colorpicker-button)") {
- document.getElementById("pick-color-rainbowpicker").style.display = "-moz-box";
- document.getElementById("pick-color").style.display = "none";
- }
- var o = window.arguments ? window.arguments[0] : {};
- if (/uri=/.test(location.href)) {
- o.uri = decodeURIComponent(location.href.substring(location.href.indexOf("uri=") + 4));
- }
- if ("uri" in o) {
- style = new GoogleRedesignedStyle(o.uri);
- newStyle = !style.code;
- if ("description" in o) {
- style.description = o.description;
- }
- descriptionElement.value = style.description;
- document.title = style.description;
- if (style.enabled) {
- appliedCode = style.code;
- }
- if ("code" in o) {
- updateOriginalCode = true;
- style.code = o.code;
- }
- if ("updateURL" in o) {
- style.updateURL = o.updateURL;
- }
- if ("installPingURL" in o) {
- installPingURL = o.installPingURL;
- }
- if ("triggeringDocument" in o) {
- triggeringDocument = o.triggeringDocument;
- }
- } else {
- document.title = STRINGS.getString("addStyle");
- style = new GoogleRedesignedStyle;
- style._enabled = true;
- if ("code" in o) {
- style.code = o.code;
- }
- newStyle = true;
- }
- initialCode = style.code;
- codeElement.value = style.code;
- enabledElement.checked = newStyle ? true : style.enabled;
- neverUpdateElement.checked = !style.neverUpdate;
- if ("windowtype" in o) {
- document.documentElement.setAttribute("windowtype", o.windowtype);
- }
- if (newStyle && style.description) {
- document.getElementById("basic-view").style.display = "-moz-box";
- document.getElementById("advanced-view").style.display = "-moz-box";
- refreshBasicDisplay();
- document.getElementById("deck").selectedIndex = prefs.getIntPref("newStyleView");
- }
- var internalURI = style.uri.substring(0, 3) == "rdf" ||
- style.uri.substring(0, 3) == "urn";
- if (internalURI) {
- neverUpdateElement.style.display = "none";
- if (newStyle) {
- document.getElementById("userstyles-link").style.display = "inline";
- } else {
- var link = document.getElementById("post-to-userstyles");
- link.setAttribute("href", URLS.getFormattedString("postToUserstylesUrl", [encodeURIComponent(style.uri)]));
- link.style.display = "inline";
- }
- } else {
- var link = document.getElementById("style-url-link");
- link.setAttribute("href", style.uri);
- link.style.display = "inline";
- }
- if (newStyle) {
- enabledElement.style.display = "none";
- }
- }
- function validate() {
- if (!previewOnly && (descriptionElement.value == null || descriptionElement.value == "")) {
- descriptionElement.focus();
- alert(STRINGS.getString("blankDescription"));
- return false;
- }
- if (codeElement.value == null || codeElement.value == "") {
- codeElement.focus();
- alert(STRINGS.getString("blankCode"));
- return false;
- }
- return true;
- }
- function cancelDialog() {
- var close = true;
- if (!googleredesignedCommon.cssAreEqual(codeElement.value, initialCode)) {
- var prompts = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService);
- var button = prompts.confirmEx(window, STRINGS.getString("discardChangesTitle"), STRINGS.getString("discardChangesMessage"), prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING, STRINGS.getString("discardChangesDiscard"), STRINGS.getString("discardChangesReturn"), null, null, {value: false});
- close = button == 0;
- }
- if (close) {
- if (appliedCode &&
- (appliedCode != style.code || newStyle || !style.enabled)) {
- googleredesignedCommon.unregisterStyle(style.uri, appliedCode);
- if (!newStyle && style.enabled) {
- style.register();
- }
- }
- }
- return close;
- }
- function refreshBasicDisplay() {
- loadAppliesTo();
- var simpleDescription = document.getElementById("simple-description");
- while (simpleDescription.firstChild) {
- simpleDescription.removeChild(simpleDescription.firstChild);
- }
- simpleDescription.appendChild(document.createTextNode(STRINGS.getFormattedString("basicIntro", [descriptionElement.value])));
- }
- function saveViewPreference() {
- prefs.setIntPref("newStyleView", document.getElementById("deck").selectedIndex);
- }
- function startLoadStylesheet() {
- var consoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
- var errorListener = new CSSErrorListener;
- var doc = setUpDocument();
- consoleService.registerListener(errorListener);
- var loadedListener = new GoogleRedesignedStylesheetLoadedListener(doc, errorListener, endStylesheetLoad);
- doc.documentElement.appendChild(setUpLink(doc));
- loadedListener.checkStyleLoaded();
- }
- function setUpDocument() {
- return document.implementation.createDocument(googleredesignedCommon.XULNS, "googleredesigned-parse", null);
- }
- function setUpLink(doc) {
- var link = doc.createElementNS(googleredesignedCommon.HTMLNS, "link");
- link.rel = "stylesheet";
- link.type = "text/css";
- link.href = googleredesignedCommon.codePrefix + codeElement.value;
- return link;
- }
- function loadAppliesTo() {
- var doc = setUpDocument();
- var loadedListener = new GoogleRedesignedStylesheetLoadedListener(doc, null, endStylesheetLoadAppliesTo);
- doc.documentElement.appendChild(setUpLink(doc));
- loadedListener.checkStyleLoaded();
- }
- function endStylesheetLoadAppliesTo(success, data) {
- if (!success) {
- return;
- }
- style.calculateMetadata(data.stylesheet);
- var appliesTo = document.getElementById("applies-to");
- while (appliesTo.firstChild) {
- appliesTo.removeChild(appliesTo.firstChild);
- }
- var a = style.appliesToDisplayArray;
- var frag = document.createDocumentFragment();
- for (var i = 0; i < a.length; i++) {
- var li = document.createElementNS(googleredesignedCommon.HTMLNS, "li");
- li.appendChild(document.createTextNode(a[i]));
- frag.appendChild(li);
- }
- appliesTo.appendChild(frag);
- }
- function cleanError(error) {
- try {
- return error.QueryInterface(Components.interfaces.nsIScriptError).errorMessage;
- } catch (ex) {
- return error.message;
- }
- }
- function endStylesheetLoad(success, data) {
- if (!success) {
- throw data.exception;
- }
- if (data.errors.length > 0) {
- var errorString;
- for (var i = 0; i < data.errors.length; i++) {
- if (errorString) {
- errorString += "\n\n" + cleanError(data.errors[i]);
- } else {
- errorString = cleanError(data.errors[i]);
- }
- }
- var prompts = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService);
- if (1 == prompts.confirmEx(window, STRINGS.getString("cssErrorDialogTitle"), STRINGS.getFormattedString("cssErrorText", [errorString]), prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING, STRINGS.getString("cssErrorAccept"), STRINGS.getString("cssErrorCancel"), null, null, {})) {
- return;
- }
- }
- if (previewOnly) {
- preview();
- return;
- }
- style.calculateMetadata(data.stylesheet);
- if (appliedCode) {
- googleredesignedCommon.unregisterStyle(style.uri, appliedCode);
- }
- if (installPingURL) {
- var req = new XMLHttpRequest;
- req.open("GET", installPingURL, true);
- req.send(null);
- }
- googleredesignedCommon.dispatchEvent(triggeringDocument, "styleInstalled");
- style.description = descriptionElement.value;
- style.code = codeElement.value;
- style.enabled = enabledElement.checked;
- style.neverUpdate = !neverUpdateElement.checked;
- if (updateOriginalCode) {
- style.originalCode = initialCode;
- }
- if (updateURL) {
- style.updateURL = updateURL;
- }
- style.customized = !googleredesignedCommon.cssAreEqual(initialCode, style.code);
- style.save();
- googleredesignedCommon.reloadManage();
- keepStyleOnClose = true;
- close();
- }
- function dialogClosing() {
- if (!keepStyleOnClose && newStyle) {
- GoogleRedesignedStyle.prototype.ds.deleteRecursive(style.node);
- GoogleRedesignedStyle.prototype.ds.save();
- }
- }
- function doPreview() {
- previewOnly = true;
- save();
- }
- function preview() {
- if (appliedCode) {
- googleredesignedCommon.unregisterStyle(style.uri, appliedCode);
- }
- appliedCode = codeElement.value;
- googleredesignedCommon.registerStyle(style.uri, codeElement.value);
- }
- function save() {
- if (!validate()) {
- return false;
- }
- startLoadStylesheet();
- return false;
- }
- function postStyle() {
- previewOnly = false;
- if (!validate()) {
- return false;
- }
- startLoadStylesheet();
- return true;
- }
- function ok() {
- previewOnly = false;
- return save();
- }
- function applySpecifySite(data) {
- if (data.length == 0) {
- return;
- }
- var selector = "";
- for (var i = 0; i < data.length; i++) {
- if (selector != "") {
- selector += ", ";
- }
- selector += data[i].type + "(" + data[i].site + ")";
- }
- selector = "@-moz-document " + selector + " {\n";
- if (codeElement.selectionStart != codeElement.selectionEnd) {
- var selection = codeElement.value.substring(codeElement.selectionStart, codeElement.selectionEnd);
- var newValue = "";
- if (codeElement.selectionStart > 0) {
- newValue = codeElement.value.substring(0, codeElement.selectionStart) + "\n";
- }
- newValue += selector;
- var newCaretPosition = newValue.length;
- newValue += selection + "\n}";
- if (codeElement.selectionEnd < codeElement.value.length) {
- newValue += "\n" + codeElement.value.substring(codeElement.selectionEnd, codeElement.value.length);
- }
- } else {
- if (codeElement.value.length > 0) {
- var newValue = codeElement.value + "\n" + selector;
- var newCaretPosition = newValue.length;
- newValue += "\n}";
- } else {
- var newValue = selector;
- var newCaretPosition = newValue.length;
- newValue += "\n}";
- }
- }
- codeElement.value = newValue;
- codeElement.setSelectionRange(newCaretPosition, newCaretPosition);
- codeElement.focus();
- }
- function CSSErrorListener() {
- this.errors = [];
- }
- CSSErrorListener.prototype = {
- QueryInterface: function(aIID) {
- if (aIID.equals(Components.interfaces.nsIConsoleListener) ||
- aIID.equals(Components.interfaces.nsISupports))
- return this;
- throw Components.results.NS_NOINTERFACE;
- },
- observe: function(message) {
- try {
- if (message.QueryInterface(Components.interfaces.nsIScriptError).sourceName.indexOf(googleredesignedCommon.codePrefix) == 0) {
- this.errors.push(message);
- }
- } catch (ex) {
- }
- }
- }
-